package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.dto.system.AssginMenuDto;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysMenu")
@Tag(name = "用户菜单模块")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("findMenu")
    @Operation(summary = "获取菜单树")
    public Result<List<SysMenu>> findMenu() {
        List<SysMenu> list = sysMenuService.findMenu();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("addMenu")
    @Operation(summary = "添加菜单方法")
    public Result addMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.addMenu(sysMenu);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("添加成功");
    }

    @GetMapping("getById/{id}")
    @Operation(summary = "根据id查询菜单信息")
    public Result<SysMenu> getById(@PathVariable Long id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.build(sysMenu, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("updateById")
    @Operation(summary = "更新菜单信息")
    public Result updateById(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("更新成功");
    }

    @DeleteMapping("deleteById/{id}")
    @Operation(summary = "删除菜单信息")
    public Result deleteById(@PathVariable Long id) {
        sysMenuService.deleteById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("删除成功");
    }

    @GetMapping("GetSysRoleMenuIds/{RoleId}")
    @Operation(summary = "查询角色的分配菜单")
    public Result<Map<String, Object>> GetSysRoleMenuIds(@PathVariable Long RoleId) {
        Map<String, Object> map = sysMenuService.toAssignMenu(RoleId);
        return Result.build(map, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("DoAssign")
    @Operation(summary = "修改角色的菜单信息")
    public Result doAssign(@RequestBody AssginMenuDto assginMenuDto) {
        sysMenuService.doAssign(assginMenuDto);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("修改成功");
    }

}
